import { GlobalRoot } from '../global/GlobalRoot';
import { ResourceManager } from '../managers/ResourceManager';
import { Node, instantiate, director, Label, Layers } from 'cc';

/**
 * Prefab 功能测试用例集合
 */
export class PrefabTest {
    static runAll(parentNode?: Node) {
        this.testLoadPrefab(parentNode);
        this.testInstantiatePrefab(parentNode);
        this.testShowAndDestroyPrefab(parentNode);
    }
    /**
     * 测试预制体加载
     * 预期：控制台输出Prefab对象
     */
    static testLoadPrefab(parentNode?: Node) {
        ResourceManager.instance.loadPrefab({ name: 'prefab/blockLayer' }, (prefab) => {
            console.log('[测试] 预制体加载完成', prefab);
            if (parentNode) {
                parentNode.removeAllChildren();
                const labelNode = new Node('PrefabLabel');
                labelNode.layer = Layers.Enum.UI_2D;
                const label = labelNode.addComponent(Label);
                label.overflow = Label.Overflow.RESIZE_HEIGHT;
                label.lineHeight = 24;
                label.fontSize = 18;
                label.string = '预制体加载完成: ' + (prefab ? '成功' : '失败');
                labelNode.setPosition(0, 0);
                parentNode.addChild(labelNode);
            }
        });
        GlobalRoot.instance.UiManager.showTips("[测试] 预制体加载完成")

    }
    /**
     * 测试预制体实例化
     * 预期：场景中出现实例化节点
     */
    static testInstantiatePrefab(parentNode?: Node) {
        ResourceManager.instance.loadPrefab({ name: 'prefab/blockLayer' }, (prefab) => {
            if (prefab) {
                const node = instantiate(prefab);
                node.setPosition(0, 0, 0);
                if (parentNode) {
                    parentNode.removeAllChildren();
                    parentNode.addChild(node);
                } else {
                    const canvas = director.getScene().getChildByName('Canvas');
                    node.parent = canvas || director.getScene();
                }
                console.log('[测试] 预制体实例化并显示', node);
            }
            GlobalRoot.instance.UiManager.showTips("[测试] 预制体实例化并显示")

        });
    }
    /**
     * 测试预制体显示与销毁
     * 预期：节点显示3秒后自动销毁
     */
    static testShowAndDestroyPrefab(parentNode?: Node) {
        ResourceManager.instance.loadPrefab({ name: 'prefab/blockLayer' }, (prefab) => {
            if (prefab) {
                const node = instantiate(prefab);
                node.setPosition(0, 100, 0);
                if (parentNode) {
                    parentNode.removeAllChildren();
                    parentNode.addChild(node);
                } else {
                    const canvas = director.getScene().getChildByName('Canvas');
                    node.parent = canvas || director.getScene();
                }
                console.log('[测试] 预制体显示3秒后销毁', node);
                GlobalRoot.instance.UiManager.showTips("[测试] 预制体显示3秒后销毁")

                setTimeout(() => {
                    node.destroy();
                    console.log('[测试] 预制体已销毁');
                    GlobalRoot.instance.UiManager.showTips("[测试] 预制体已销毁")

                }, 3000);
            }
        });
    }
} 